package com.clouddrive.dao.impl;

import com.clouddrive.User;
import com.clouddrive.dao.UserDao;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public User queryUserByEmail(String email) {
        String sql="SELECT `number`,`email`,`username`,`password` " +
                "FROM userdata " +
                "WHERE email = ?";
        return queryForOne(User.class,sql,email);
    }

    @Override
    public User queryUserByEmailAndPassword(String email, String password) {
        String sql="SELECT `number`,`email`,`username`,`password` " +
                "FROM userdata " +
                "WHERE email = ? " +
                "AND password = ?";
        return queryForOne(User.class,sql,email,password);
    }
    @Override
    public User queryUserByEmailAndUsername(String email, String username){
        String sql="SELECT `number`,`email`,`username`,`password` " +
                "FROM userdata " +
                "WHERE email = ? " +
                "AND username = ?";
        return queryForOne(User.class,sql,email,username);
    }
    @Override
    public User updateUserInformation(User user){
        String sql="UPDATE userdata " +
                "SET username=?, password=? " +
                "WHERE email = ?";
        update(sql,user.getUsername(),user.getPassword(),user.getEmail());
        return queryUserByEmail(user.getEmail());
    }
    @Override
    public User saveUser(User user) {
        String sql="INSERT INTO `myclouddrive`.`userdata` (`Email`, `Username`, `Password`) VALUES (?, ?, ?);";
        update(sql, user.getEmail(),user.getUsername(),user.getPassword());
        return queryUserByEmail(user.getEmail());
    }
}
